//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.hustcad.plm.pdm.workflow.controller;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hustcad.plm.basic.log.LogEventEnum;
import com.hustcad.plm.basic.log.TyplmLogger;
import com.hustcad.plm.pdm.workflow.model.constant.WorkFlowErrorCodeEnum;
import com.hustcad.plm.pdm.workflow.model.dto.AdvancedFlowContainerCondition;
import com.hustcad.plm.pdm.workflow.model.entity.FlowTaskInfo;
import com.hustcad.plm.pdm.workflow.model.entity.UserTaskDTO;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainer;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainerView;
import com.hustcad.plm.pdm.workflow.model.vo.AdvancedFlowContainerVO;
import com.hustcad.plm.pdm.workflow.model.vo.CodeStrategyTypeVO;
import com.hustcad.plm.pdm.workflow.model.vo.UpdateUserTaskVO;
import com.hustcad.plm.pdm.workflow.service.TyplmFlowCommonService;
import com.hustcad.plm.pdm.workflow.service.TyplmFlowContainerObjectService;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowCommonViewService;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowProcessService;
import com.ty.basic.entity.CTyEntityBaseDO;
import com.ty.basic.entity.audit.AuditLogDO;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.exception.message.SystemErrorCodeEnum;
import com.ty.basic.response.ResponseResult;
import com.ty.basic.session.TyAccountContext;
import com.ty.basic.utils.ArgumentUtils;
import com.ty.basic.utils.controller.CommonQuery;
import com.ty.basic.utils.controller.ControllerUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(
        tags = {"流程容器服务"}
)
@RequestMapping({"/rest/v1/workFlowContainer"})
public class TyplmWorkFlowContainerController {
    @Resource
    private TyplmWorkFlowContainerService typlmWorkFlowContainerService;
    @Resource
    private TyplmFlowCommonService typlmFlowCommonService;
    @Resource
    private TyplmWorkFlowProcessService typlmWorkFlowProcessService;
    @Resource
    private TyplmFlowContainerObjectService typlmFlowContainerObjectService;
    @Resource
    private TyplmLogger typlmLogger;
    @Resource
    private TyplmWorkFlowCommonViewService typlmWorkFlowCommonViewService;

    public TyplmWorkFlowContainerController() {
    }

    @PostMapping({"/advancedQueryListPage"})
    @ApiOperation(
            value = "流程高级查询",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<PageInfo<AdvancedFlowContainerVO>> advancedQueryListPage(@RequestBody AdvancedFlowContainerCondition condition) {
        PageInfo<AdvancedFlowContainerVO> pageInfo = this.typlmWorkFlowContainerService.advancedQueryListPage(condition);
        return ResponseResult.success(pageInfo);
    }

    @PostMapping({"/queryMyCreateWorkFlowCount"})
    @ApiOperation(
            value = "我创建的流程数量",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Integer> queryMyCreateWorkFlowCount() {
        return ResponseResult.success(this.typlmWorkFlowContainerService.queryMyCreateWorkFlowCount());
    }

    @PostMapping({"/queryMyJoinWorkFlowCount"})
    @ApiOperation(
            value = "我参与的流程数量",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Integer> queryMyJoinWorkFlowCount() {
        return ResponseResult.success(this.typlmWorkFlowContainerService.queryMyJoinWorkFlowCount());
    }

    /** @deprecated */
    @Deprecated
    @PostMapping({"/advancedQueryList"})
    @ApiOperation(
            value = "流程高级查询",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<PageInfo<AdvancedFlowContainerVO>> advancedQueryList(@RequestBody AdvancedFlowContainerCondition containerCondition) {
        PageInfo<AdvancedFlowContainerVO> pageInfo = this.typlmWorkFlowContainerService.advancedQueryListPage(containerCondition);
        return ResponseResult.success(pageInfo);
    }

    @PostMapping({"/queryRecentFlowManageAndSupervisor"})
    @ApiOperation(
            value = "查询最新选择的管理者和监控者",
            notes = "查询最新选择的管理者和监控者",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<WorkFlowContainerView> queryRecentFlowManageAndSupervisor(@RequestBody JSONObject reqJson) {
        BigInteger userOid = reqJson.getBigInteger("userOid");
        ArgumentUtils.checkArgumentEmpty(userOid, "userOid");
        BigInteger workFlowId = reqJson.getBigInteger("workFlowId");
        //ArgumentUtils.checkArgumentEmpty(workFlowId, "workFlowId");
        BigInteger containerOid = reqJson.getBigInteger("containerOid");
        ArgumentUtils.checkArgumentEmpty(containerOid, "containerOid");
        String containerOtype = reqJson.getString("containerOtype");
        ArgumentUtils.checkArgumentEmpty(containerOtype, "containerOtype");
        WorkFlowContainerView workFlowContainerView = this.typlmFlowCommonService.queryRecentFlowManageAndSupervisor(userOid, workFlowId, containerOid, containerOtype);
        return ResponseResult.success(workFlowContainerView);
    }

    @PostMapping({"/myDraftWorkFlow"})
    @ApiOperation(
            value = "我的流程草稿",
            notes = "我的流程草稿",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult myDraftWorkFlow(@RequestBody CommonQuery commonQuery) {
        this.typlmWorkFlowContainerService.reSetCondition(commonQuery);
        return ControllerUtils.queryTarget(commonQuery, this.typlmWorkFlowContainerService);
    }

    @GetMapping({"/getAllUserTask"})
    @ApiOperation(
            value = "根据流程模板所有的用户任务节点",
            notes = "根据流程模板所有的用户任务节点",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<List<UserTaskDTO>> getAllUserTask(BigInteger workFlowId, @RequestParam(value = "containerOid",required = false) BigInteger containerOid, @RequestParam(value = "containerOtype",required = false) String containerOtype) {
        List<UserTaskDTO> userTaskDTOList = this.typlmWorkFlowContainerService.getAllUserTask(workFlowId, containerOid, containerOtype);
        return ResponseResult.success(userTaskDTOList);
    }

    @GetMapping({"/getWorkFlowContainerDetailByPrimaryKey"})
    @ApiOperation(
            value = "根据主键获取流程容器的详细信息",
            notes = "根据主键获取流程容器的详细信息",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<WorkFlowContainerView> getWorkFlowContainerDetailByPrimaryKey(BigInteger key) {
        try {
            WorkFlowContainerView workFlowContainerView = this.typlmWorkFlowContainerService.getWorkFlowContainerDetailByPrimaryKey(key);
            if (workFlowContainerView != null) {
                this.typlmLogger.info(new CTyEntityBaseDO(workFlowContainerView.getOid(), workFlowContainerView.getOtype()), LogEventEnum.VIEW_OBJECT);
            }

            return ResponseResult.success(workFlowContainerView);
        } catch (Exception var3) {
            this.typlmLogger.errorInfo(new CTyEntityBaseDO(key, "com.ty.entity.workFlow.WorkFlowContainer"), LogEventEnum.VIEW_OBJECT, var3);
            throw var3;
        }
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @DeleteMapping({"/deleteWorkFlowContainerByPrimaryKey"})
    @ApiOperation(
            value = "根据主键删除流程容器",
            notes = "根据主键删除流程容器",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Boolean> deleteWorkFlowContainerByPrimaryKey(BigInteger key) {
        try {
            this.typlmLogger.info(new CTyEntityBaseDO(key, "com.ty.entity.workFlow.WorkFlowContainer"), LogEventEnum.DELETE_OBJECT);
            this.typlmWorkFlowContainerService.delete(key);
            this.typlmFlowContainerObjectService.deleteRuFlowObjectByFlowContainer(key);
            return ResponseResult.success(true);
        } catch (Exception var3) {
            this.typlmLogger.errorInfo(new CTyEntityBaseDO(key, "com.ty.entity.workFlow.WorkFlowContainer"), LogEventEnum.DELETE_OBJECT, var3);
            throw var3;
        }
    }

    @DeleteMapping({"/deleteWorkFlowContainerByPrimaryKeyCollection"})
    @ApiOperation(
            value = "根据主键集合删除流程容器",
            notes = "根据主键集合删除流程容器",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Boolean> deleteWorkFlowContainerByPrimaryKeyCollection(@RequestBody Set<BigInteger> keys) {
        try {
            Iterator var2 = keys.iterator();

            while(var2.hasNext()) {
                BigInteger key = (BigInteger)var2.next();
                this.typlmLogger.info(new CTyEntityBaseDO(key, "com.ty.entity.workFlow.WorkFlowContainer"), LogEventEnum.DELETE_OBJECT);
            }

            this.typlmWorkFlowContainerService.delete(keys);
            return ResponseResult.success(true);
        } catch (Exception var4) {
            AuditLogDO auditLogDO = new AuditLogDO();
            auditLogDO.setObjotype("com.ty.entity.workFlow.WorkFlowContainer");
            this.typlmLogger.errorInfo(LogEventEnum.DELETE_OBJECT, auditLogDO, var4);
            throw var4;
        }
    }

    @PostMapping({"/queryFlowByBaseObjectOid"})
    @ApiOperation(
            value = "根据容器上下文查询流程实例",
            notes = "根据容器上下文查询流程实例",
            produces = "application/json; charset=utf-8"
    )
    public PageInfo<FlowTaskInfo> queryFlowByContainer(@RequestBody JSONObject input) {
        WorkFlowContainer container = new WorkFlowContainer();
        container.setBaseObjectOid(input.getBigInteger("baseObjectOid"));
        return this.typlmWorkFlowProcessService.queryFlowByBaseObjectOid(container, input.getIntValue("page"), input.getIntValue("limit"));
    }

    @GetMapping({"/viewContainerFlowChart/{workFlowContainerId}"})
    @ApiOperation(
            value = "预览缩略图",
            produces = "application/json; charset=utf-8"
    )
    public void viewContainerFlowChart(@PathVariable("workFlowContainerId") BigInteger workFlowContainerId, HttpServletResponse response) {
        response.setContentType("image/png");
        WorkFlowContainer workFlowContainer = (WorkFlowContainer)this.typlmWorkFlowContainerService.selectOneByOid(workFlowContainerId);

        try {
            InputStream in = workFlowContainer.getStatus() == 0 ? this.typlmWorkFlowContainerService.viewContainerModelThumbnail(workFlowContainer) : this.typlmWorkFlowProcessService.getFlowChart(workFlowContainer);
            Throwable var5 = null;

            try {
                OutputStream out = response.getOutputStream();
                Throwable var7 = null;

                try {
                    byte[] b = new byte[1024];

                    int len;
                    while((len = in.read(b, 0, 1024)) != -1) {
                        out.write(b, 0, len);
                    }
                } catch (Throwable var33) {
                    var7 = var33;
                    throw var33;
                } finally {
                    if (out != null) {
                        if (var7 != null) {
                            try {
                                out.close();
                            } catch (Throwable var32) {
                                var7.addSuppressed(var32);
                            }
                        } else {
                            out.close();
                        }
                    }

                }
            } catch (Throwable var35) {
                var5 = var35;
                throw var35;
            } finally {
                if (in != null) {
                    if (var5 != null) {
                        try {
                            in.close();
                        } catch (Throwable var31) {
                            var5.addSuppressed(var31);
                        }
                    } else {
                        in.close();
                    }
                }

            }

        } catch (IOException var37) {
            throw SystemErrorCodeEnum.MODEL_THUMBNAIL_VIEW_FAIL.getException(var37, new Object[0]);
        }
    }

    @GetMapping({"/queryTaskByFlowContainer/{workFlowContainerId}"})
    @ApiOperation(
            value = "根据流程容器查询流程任务",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Map<String, List<FlowTaskInfo>>> queryTaskByFlowContainer(@PathVariable("workFlowContainerId") BigInteger workFlowContainerId) {
        return ResponseResult.success(this.typlmWorkFlowContainerService.queryTaskByFlowContainer(workFlowContainerId));
    }

    @PostMapping({"/excelExport"})
    @ApiOperation(
            value = "导出excel",
            notes = "导出excel",
            produces = "application/json; charset=utf-8"
    )
    public void excelExport(@RequestBody List<BigInteger> oidList, HttpServletResponse response) throws IOException {
        AdvancedFlowContainerCondition condition = new AdvancedFlowContainerCondition();
        condition.setOidSet(new HashSet(oidList));
        Set<BigInteger> oidSet = condition.getOidSet();
        if (CollUtil.isEmpty(oidSet)) {
            throw WorkFlowErrorCodeEnum.CHOOSE_OBJECT_IS_EMPTY.getException(new Object[0]);
        } else if (oidSet.size() > 1000) {
            throw WorkFlowErrorCodeEnum.CHOOSE_OBJECT_NO_MORE_THAN_MAX.getException(new Object[]{"1000"});
        } else {
            this.typlmWorkFlowContainerService.exportFlowListByCondition(condition, response);
            UserDO currentUser = TyAccountContext.getUser();
            this.typlmLogger.info(new CTyEntityBaseDO(currentUser.getOid(), currentUser.getOtype()), LogEventEnum.DOWNLOAD, "导出所选流程列表");
        }
    }

    @PostMapping({"/allExcelExport"})
    @ApiOperation(
            value = "全部导出excel",
            notes = "全部导出excel",
            produces = "application/json; charset=utf-8"
    )
    public void allExcelExport(@RequestBody AdvancedFlowContainerCondition condition, HttpServletResponse response) throws IOException {
        this.typlmWorkFlowContainerService.exportFlowListByCondition(condition, response);
        UserDO currentUser = TyAccountContext.getUser();
        this.typlmLogger.info(new CTyEntityBaseDO(currentUser.getOid(), currentUser.getOtype()), LogEventEnum.DOWNLOAD, "导出全部流程列表");
    }

    @GetMapping({"/refreshWorkFlowSignInfo"})
    @ApiOperation(
            value = "刷新流程签名记录",
            produces = "application/json; charset=utf-8"
    )
    public void refreshWorkFlowSignInfo() {
        this.typlmWorkFlowContainerService.refreshWorkFlowSignInfo();
    }

    @PostMapping({"/getCodeRuleByDeploymentId"})
    @ApiOperation(
            value = "获取编码信息",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<CodeStrategyTypeVO> getCodeRuleByDeploymentId(@RequestBody String deploymentId) {
        return ResponseResult.success(this.typlmWorkFlowContainerService.getCodeRuleByDeploymentId(deploymentId));
    }

    @PostMapping({"/modifySubSequentExecutor"})
    @ApiOperation(
            value = "修改后续执行人",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Boolean> modifySubSequentExecutor(@RequestBody UpdateUserTaskVO updateUserTaskVO) {
        this.typlmWorkFlowContainerService.modifySubSequentExecutor(updateUserTaskVO);
        return ResponseResult.success();
    }

    @GetMapping({"/getUserTaskWithOutActived"})
    @ApiOperation(
            value = "根据流程模板非运行的用户任务节点",
            notes = "根据流程模板非运行的用户任务节点",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<List<UserTaskDTO>> getUserTaskWithOutActived(@RequestParam(value = "containerOid",required = false) BigInteger containerOid, @RequestParam(value = "containerOtype",required = false) String containerOtype) {
        List<UserTaskDTO> userTaskDTOList = this.typlmWorkFlowContainerService.getUserTaskWithOutActived(containerOid, containerOtype);
        return ResponseResult.success(userTaskDTOList);
    }

    @GetMapping({"/getOptionalParticipantUserTask"})
    @ApiOperation(
            value = "根据流程模板所有的用户任务节点",
            notes = "根据流程模板可选（包含可选和必须）的用户任务节点",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<List<UserTaskDTO>> getOptionalParticipantUserTask(String flowableProcessId, BigInteger workFlowId, @RequestParam(value = "containerOid",required = false) BigInteger containerOid, @RequestParam(value = "containerOtype",required = false) String containerOtype) {
        List<UserTaskDTO> userTaskDTOList = this.typlmWorkFlowContainerService.getOptionalParticipantUserTask(flowableProcessId, workFlowId, containerOid, containerOtype, Boolean.TRUE, Boolean.FALSE);
        return ResponseResult.success(userTaskDTOList);
    }

    @PostMapping({"/getCommonEmptyWorkFlowContainerView"})
    @ApiOperation(
            value = "根据流程模板获取空的流程容器视图",
            notes = "根据流程模板获取空的流程容器视图",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<WorkFlowContainerView> getEmptyWorkFlowContainerView(@RequestBody WorkFlowContainerView workFlowContainerView) {
        this.typlmWorkFlowCommonViewService.getEmptyWorkFlowContainerView(workFlowContainerView);
        return ResponseResult.success(workFlowContainerView);
    }

    @GetMapping({"/getWorkFlowUserTask"})
    @ApiOperation(
            value = "根据流程模板id查询用户任务",
            notes = "根据流程模板id查询用户任务",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<List<UserTaskDTO>> getWorkFlowUserTask(BigInteger workFlowOid, String taskdefkey) {
        List<UserTaskDTO> userTaskList = this.typlmWorkFlowContainerService.getWorkFlowUserTask(workFlowOid, taskdefkey);
        return ResponseResult.success(userTaskList);
    }
}
